import msg from "../../../utils/msg";
import { activity_list, getGameList } from "../../../utils/request/api";
import User from "../../../utils/user";
import {filePath1} from "../../../utils/request/route";
// pages/user/myJoin/myJoin.ts
Page({

  /**
   * 页面的初始数据
   */
  data: {
    windowHeight:0,
    showList:[],
    list:[],
    searchList:[],
    act:[],
    game:[],
    tabSearch:'1',
    inputSearch:'',
    filePath1,
  },
  //赛事
  goToDetail(e:any){
    let index = e.currentTarget.dataset.index;
    let data:any = this.data.showList[index];
    wx.navigateTo({
      url:"/pages/compete/comDetail/comDetail"+'?id='+data.id,
    })
  },
  goToDetailAct(e:any){
    let index = e.currentTarget.dataset.index;
    let data:any = this.data.showList[index];
    wx.navigateTo({
      url:"/pages/act/actDetail/actDetail"+'?id='+data.id,
    });
  },
  getInputSearch(e: any) {
    let input = e.detail;
    this.setData({
      inputSearch: input,
    });
    this.search();
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: async function () {
    let user = new User;
    this.setTopHeight();
    //获取已报名活动列表
    msg.Loading();
    let res_act:any = await activity_list({GF_ID:user.GF_ID,type:2});
    let res_game:any = await getGameList({type:2,visit_gfid:user.GF_ID});
    msg.hideLoading();
    this.setData({
      act:res_act.data.datas,
      game:res_game.data.datas,
    });
    this.search();
  },
  //tab筛选
  onChange(e:any){
    let index = e.detail.index;
    let tabSearch="";
    switch(index){
      case 0: tabSearch='1';break;
      case 1: tabSearch='2';break;
    }
    this.setData({
      tabSearch,
    });
    this.search();
  },

  search(){
    this.setData({
      searchList:(this.data.tabSearch=='1')?this.data.game:this.data.act,
    });
    //各类搜索
    this.inputSearch();
    //重置显示数据
    this.setData({showList:[]});
    this._pushShowData();
  },
  //渲染新数据
  _pushShowData() {
    let data = this.data.searchList;
    let showData = this.data.showList;
    if (data.length <= showData.length) return;
    let start = showData.length;
    for (let i = start; (i < start + 20 && i < data.length); i++)
      showData.push(data[i]);
    this.setData({
      showList: showData,
    });
  },
  inputSearch() {
    let searchList:any = this.data.searchList;
    let input = this.data.inputSearch;
    if(input){
      let data = searchList;
      searchList=[];
      data.forEach((item:any)=>{
        console.log('item=',item);
        if(item.name.includes(input)||item.address.includes(input))
          searchList.push(item);
      });
    }
    this.setData({
      searchList:searchList,
    });
  },
  setTopHeight() {
    let windowHeight = wx.getSystemInfoSync().windowHeight;
    this.setData({
      windowHeight: windowHeight,
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})